package com.scud.cart.controller;

import com.scud.cart.service.CartService;
import com.scud.common.result.Result;
import com.scud.model.cart.vo.CartItemVo;
import com.scud.model.cart.dto.AddCartDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author vermouth
 */
@RestController
@RequestMapping("/api/cart/cart")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 获取当前用户购物车列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<CartItemVo>> cartList(){
        log.info("获取当前用户购物车列表");
        List<CartItemVo> cartItemVoList = cartService.cartList();
        return Result.success(cartItemVoList);
    }


    /**
     * 添加购物车
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @PostMapping("add")
    public Result addCart(@RequestBody AddCartDto dto){
        log.info("添加购物车：{}",dto);
        cartService.addCart(dto);
        return Result.success();
    }

    /**
     * 根据id删除当前用户购物车中商品
     * @return
     */
    @DeleteMapping("/removeSku/{id}")
    public Result removeSku(@PathVariable Long id){
        log.info("根据id删除当前用户购物车中商品:{}",id);
        cartService.removeSku(id);
        return Result.success();
    }

    /**
     * 清空当前用户的购物车
     * @return
     */
    @DeleteMapping("/clearCart")
    public Result clearCart(){
        log.info("清空当前用户的购物车");
        cartService.clearCart();
        return Result.success();
    }
}
